package com.smiletrain.services;

import com.smiletrain.activities.PostFotosActivity;
import com.smiletrain.base.Alarma;
import com.smiletrain.util.AdminAlarma;
import com.smiletrain.util.Constantes;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class AlertaService extends Service {
	/**
	 * @see android.app.Service#onBind(Intent)
	 */
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		
		int alarma_id = intent.getIntExtra("com.smiletrain.alarm.id", 0);
		
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		
		int icon = R.drawable.ic_notification_overlay;
		CharSequence tickerText = "Fotos Post-Operatorias";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		
		Context context = getApplicationContext();
		CharSequence contentTitle = "Fotos Post-Operatorias";
		CharSequence contentText = "Es hora de tomar las fotos Post-Operatorias!";
		Intent notificationIntent = new Intent(this, PostFotosActivity.class);
		
		Alarma alarma = AdminAlarma.instance.getAlarma(alarma_id);
		if(alarma != null)
			notificationIntent.putExtra("com.smiletrain.alarm.caso.id", alarma.getCaso().getId());
		
		PendingIntent contentIntent = PendingIntent.getActivity(
				this, Constantes.ACTIVITY_TAKE_PICTURE_VIEW04_IMG04, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_SOUND;
		
		mNotificationManager.notify(HELLO_ID, notification);
		
		AdminAlarma.instance.eliminarAlarma(alarma_id);
		
	} 
	
	private static final int HELLO_ID = 1;
	
}
